package org.apache.lucene.analysis.jjs;

import java.io.File;
import java.io.FileReader;
import java.io.Reader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class CKIPAnalyzer extends Analyzer{

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{		
		File f = new File("file");
		Directory dir = FSDirectory.open(new File("index"));
		// Here we use CKIPAnalyzer in indexing
		IndexWriterConfig config = new IndexWriterConfig(
				                       		Version.LUCENE_36, 
				                       		new CKIPAnalyzer()  
				                       );
		IndexWriter writer = new IndexWriter(dir, config);
		Document doc = new Document();
		doc.add(new Field("contents", new FileReader(f))); 
		doc.add(new Field("filename", 
				          f.getName(), 
				          Field.Store.YES, 
				          Field.Index.NOT_ANALYZED));
		doc.add(new Field("fullpath", 
				          f.getCanonicalPath(), 
				          Field.Store.YES, 
				          Field.Index.NOT_ANALYZED));
		
		writer.addDocument(doc);
		writer.close();
	}

	@Override
	public TokenStream tokenStream(String fieldName, Reader reader) {
		TokenStream ts = new CKIPSegTokenizer(reader);
		return ts;
	}

}
